import logging
import time

try:
    from urllib import parse as urlparse
except ImportError:
    import urlparse


def connect_message_queue(name, url=None, maxsize=0, lazy_limit=True):
    """
    create connection to message queue

    name:
        name of message queue

    rabbitmq:
        amqp://username:password@host:5672/%2F
        see https://www.rabbitmq.com/uri-spec.html
    redis:
        redis://host:6379/db
        redis://host1:port1,host2:port2,...,hostn:portn (for redis 3.x in cluster mode)
    kombu:
        kombu+transport://userid:password@hostname:port/virtual_host
        see http://kombu.readthedocs.org/en/latest/userguide/connections.html#urls
    builtin:
        None
    """
    from utils.msg_queue.redis_queue import Queue
    parsed = urlparse.urlparse(url)
    # '/1'使用斜杠作为分隔符将路径部分分隔为一个字符串列表['1']
    db = parsed.path.lstrip('/').split('/')
    try:
        db = int(db[0])
    except:
        logging.warning('redis DB must zero-based numeric index, using 0 instead')
        db = 0
    password = parsed.password or None
    return Queue(name=name, host=parsed.hostname, port=parsed.port, db=db, maxsize=maxsize, password=password,
                 lazy_limit=lazy_limit)
    # if parsed.scheme == 'amqp':
    #     from .rabbitmq import Queue
    #     return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
    # elif parsed.scheme == 'redis':
    #     from .redis_queue import Queue
    #     if ',' in parsed.netloc:
    #         """
    #         redis in cluster mode (there is no concept of 'db' in cluster mode)
    #         ex. redis://host1:port1,host2:port2,...,hostn:portn
    #         """
    #         cluster_nodes = []
    #         for netloc in parsed.netloc.split(','):
    #             cluster_nodes.append({'host': netloc.split(':')[0], 'port': int(netloc.split(':')[1])})
    #
    #         return Queue(name=name, maxsize=maxsize, lazy_limit=lazy_limit, cluster_nodes=cluster_nodes)
    #
    #     else:
    #         db = parsed.path.lstrip('/').split('/')
    #         try:
    #             db = int(db[0])
    #         except:
    #             logging.warning('redis DB must zero-based numeric index, using 0 instead')
    #             db = 0
    #
    #         password = parsed.password or None
    #
    #         return Queue(name=name, host=parsed.hostname, port=parsed.port, db=db, maxsize=maxsize, password=password, lazy_limit=lazy_limit)
    # elif url.startswith('kombu+'):
    #     url = url[len('kombu+'):]
    #     from .kombu_queue import Queue
    #     return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
    # else:
    #     raise Exception('unknown connection url: %s', url)


# di = {
#     "order_id": "56d3febc981d74ac964ac3375a0d9f84",
#     "short_title":"介绍一下K8S",
#     "user_id":"123",
# }
# q = connect_message_queue(queue_name, url="redis://192.168.1.22:6379/1", maxsize=10, lazy_limit=True)
#q.put(di)




